package com.shardingdemo.aop;


import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.shardingsphere.api.hint.HintManager;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.ShardingDataSource;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.mapper.MapperFactoryBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.util.Map;

/**
 * Description: 数据源强制路由切面，在进入DAO之前，强制根据省分库方案，使用sharding-jdbc实现强制路由
 */
@Component
@Aspect
public class DatasourceRouteAspect {
    private static final Logger LOGGER = LoggerFactory.getLogger(DatasourceRouteAspect.class);

    @Autowired
    private DataSource dataSource;

    /**
     * DAO层切面
     */
    @Pointcut("execution( * com.shardingdemo.dao.*Mapper.*(..))")
    public void pointCut() {
    }

    @Before("pointCut()")
    public void before(JoinPoint joinPoint) {
//
//        String code = RequestShardValueHolder.get();
//
//        ShardingDataSource dataSource = (ShardingDataSource) this.dataSource;
//        Map<String, DataSource> dataSourceMap = dataSource.getDataSourceMap();
//        DataSource currentDataSource = dataSourceMap.get(code);

    }

    /**
     * 完成之后清理shardvalue
     *
     * @param joinPoint
     */
    @After("pointCut()")
    public void after(JoinPoint joinPoint) {
//        HintManager hintManager = HintManager.getInstance();
//        hintManager.close();
    }
}
